#-*- coding: big5 -*-
import re
from modelRepository import modelRepository
from page import Page
from log import log

class PageBuilderDirector:
    def __init__(self):
        self.lstPage=[]
        self.intPageCursor=0
    def getNextPageString(self):
        self.intPageCursor+=1
        return self.lstPage[self.intPageCursor].strPageContent
    def getPreviousPageString(self):
        self.intPageCursor-=1
        return self.lstPage[self.intPageCursor].strPageContent
    def construct(self):
        book=modelRepository.getModelBook()
        iteratorBookComponent=IteratorBookComponent(book)
        pageBuilder=PageBuilder()
        while iteratorBookComponent.hasNext():
            bookComponent=iteratorBookComponent.getNext()
            buildHandler=getBuildHandlerByBookComponent(bookComponent)
            buildHandler.build(bookComponent,pageBuilder)
        self.lstPage=pageBuilder.getLstPage()
    def getPageString(self,intPageNum):
        return self.lstPage[intPageNum].strPageContent

def getBuildHandlerByBookComponent(bookComponent):
    strClassName=re.findall(r'model\.(.*?) ',str(bookComponent))[0]
    strEval='BuildHandler%s()'%strClassName
    return eval(strEval)

class IteratorBookComponent:
    def __init__(self,book):
        self.book=book
        self.lstBookComponent=[]
        self.intBookComponentCursor=-1
        self.assignLstBookComponent()
    def hasNext(self):
        if (self.intBookComponentCursor+1)<len(self.lstBookComponent):
            return True
        return False
    def getNext(self):
        self.intBookComponentCursor+=1
        return self.lstBookComponent[self.intBookComponentCursor]
    def traverse(self,lstBookComponent):
       if not lstBookComponent: return
       for bookComponent in lstBookComponent:
           if bookComponent.isLeaf():
               self.lstBookComponent.append(bookComponent)
           else:
               self.lstBookComponent.append(bookComponent)
               self.traverse(bookComponent.lstBookComponent)
    def assignLstBookComponent(self):
        book=modelRepository.getModelBook()
        self.lstBookComponent.append(book)
        self.traverse(book.lstBookComponent)

class PageBuilder:
    def __init__(self):
        self.intMaxHeight=3
        self.page=Page()
        self.lstPage=[]
    def addLine(self,strLine):
        if not self.page.canAddLine():
            self.lstPage.append(self.page)
            self.page=Page()
        self.page.addLine(strLine)
    def getLstPage(self):
        return self.lstPage
    
class BuildHandler:
    def build(self,bookComponent,pageBuilder):
        pageBuilder.addLine(bookComponent.strText)
class BuildHandlerBook(BuildHandler):
    def build(self,bookComponent,pageBuilder):
        book=bookComponent
        pageBuilder.addLine('Book Title:%s'%book.strText)
        pageBuilder.addLine('Book Author:%s'%book.strAuthor)        
class BuildHandlerParagraph(BuildHandler):
    pass
class BuildHandlerBulletItem(BuildHandler):
    pass
class BuildHandlerUnorderedList(BuildHandler):
    pass
class BuildHandlerSection(BuildHandler):
    pass
class BuildHandlerChapter(BuildHandler):
    pass
